<template>
  <div class="vue-box">
    <div class="c-panel">
      <!-- ------------- 检索参数 ------------- -->
      <h4 class="c-title">检索参数</h4>
      <el-form @submit.native.prevent>
        <sa-item v-model="p.search" type="text" name="模糊搜索" />
        <sa-item name="角色">
          <el-select v-model="p.roleId" clearable>
            <el-option v-for="role in roleList" :key="role.id" :label="role.name" :value="role.id" />
          </el-select>
        </sa-item>
        <sa-item v-model="p.status" type="enum" jtype="4" :jv="dims.YES_OR_NO" name="启用状态" :clearable="true" />
        <div class="c-item">
          <el-button type="primary" icon="el-icon-search" @click="p.current = 1; f5()">查询</el-button>
        </div>
      </el-form>
      <!-- ------------- 快捷按钮 ------------- -->
      <sa-item type="fast-btn" show="add,get,delete,export,reset" />
      <!-- ------------- 数据列表 ------------- -->
      <el-table ref="data-table" class="data-table" :data="dataList" border>
        <sa-td type="selection" />
        <sa-td type="text" name="序号" prop="sort" width="80px" />
        <sa-td type="user-avatar" name="昵称" prop="nickName,avatar" />
        <sa-td type="text" name="用户名" prop="name" />
        <sa-td type="text" name="手机" prop="phone" />
        <sa-td type="text" name="所属角色" prop="roleName" />
        <sa-td type="text" name="登录IP" prop="loginIp" not="无" />
        <sa-td type="enum" name="是否登录" prop="loginStatus" :jv="dims.YES_OR_NO" not="无" />
        <sa-td type="enum" name="启用状态" prop="status" :jv="dims.YES_OR_NO" />
        <sa-td type="datetime" name="创建时间" prop="createTime" />

        <sa-td name="操作" fixed="right" width="160px">
          <template slot-scope="s">
            <div class="admin_oper">
              <el-button type="text" class="c-btn" @click="getInfo(s.row)"><span>查看</span></el-button>
              <el-button type="text" class="c-btn" @click="update(s.row)"><span>变更 </span></el-button>
              <el-button type="text" class="c-btn" @click="resetPwd(s.row)"
                v-if="s.row.id != sa.$sys.getCurUser().id"><span>重置密码</span></el-button>
              <el-button type="text" class="c-btn" @click="del(s.row)" v-if="s.row.id != sa.$sys.getCurUser().id">
                <span style="color: red;">删除</span></el-button>
            </div>
          </template>
        </sa-td>
      </el-table>
      <!-- 分页 -->
      <div v-if="dataCount">
        <sa-item type="page" :curr.sync="p.current" :size.sync="p.size" :total="dataCount" @change="f5()" />
      </div>
    </div>

  </div>
</template>

<script>
export default {
  name: 'acc-list',
  data() {
    return {
      // 查询参数
      p: {
        current: 1,
        size: 10,
        search: null,
        status: null,
        roleId: null,
        sid: sa.$sys.getCurSid(),
        sortField: null,
      },
      dataCount: null,
      // 数据集合
      dataList: [],
      // 角色集合
      roleList: [],
      // 缓存字典
      dims: {},
    }
  },
  created: function () {
    this.dims = sa.$sys.getCurd()
    this.f5();
    // 监听回车执行查询
    sa.onInputEnter();
    // 加载角色
    sa.ajax('/role/options', function (res) {
      this.roleList = res.data;
    }.bind(this), {
      type: 'get'
    });

  },
  methods: {
    // 刷新
    f5: function () {
      sa.ajax('/sso/acc/getList', this.p, function (res) {
        // 数据
        this.dataList = res.data;
        this.dataCount = res.dataCount;
        // 刷新表格高度
        sa.f5TableHeight();
      }.bind(this));
    },
    // 新增
    add: function () {
      sa.showModel('添加', () => import('./add'), {
        roleList: this.roleList,
        dims: this.dims
      });
    },
    // 查看详情
    getInfo: function (data) {
      sa.showModel('详情', () => import('./info'), {
        id: data.id,
        dims: this.dims
      });
    },
    // 查看 - 根据选中的
    getBySelect: function (data) {
      var selection = this.$refs['data-table'].selection;
      if (selection.length === 0) {
        return sa.msg('请至少选择一条数据')
      }
      this.getInfo(selection[0]);
    },
    update(data) {
      data.roleList = this.roleList
      data.dims = this.dims
      sa.showModel('变更', () => import('./update'), data);
    },
    resetPwd(data) {
      sa.confirm('确认重置密码，此操作不可撤销', function () {
        sa.ajax('/sso/password/reset', {
          id: data.id
        }, function (res) {
          sa.ok(res.msg);
          sa.f5TableHeight(); // 刷新表格高度
        }.bind(this), {
          type: 'get'
        })
      }.bind(this));
    },
    // 变更名称
    updateName: function (data) {
      sa.layer.confirm({
        title: '变更账号名称'
      }, function (pass, index) {
        sa.layer.close(index);
        sa.ajax('/sso/acc/update', {
          id: data.id,
          name: pass
        }, function (res) {
          data.name = pass;
          sa.msg(res.msg);
        })
      });
    },
    // 变更头像
    updateAvatar: function (data) {
      sa.uploadImage(function (src) {
        sa.ajax('/sso/acc/updateAvatar', {
          id: data.id,
          avatar: src
        }, function (res) {
          sa.msg('上传成功');
          data.avatar = src;
        }, {
          type: "get"
        });
      })
    },
    // 变更密码
    updatePassword: function (data) {
      layer.prompt({
        title: '变更密码'
      }, function (pass, index) {
        layer.close(index);
        if (pass.length < 4) {
          return layer.msg('新密码长度请不要低于4位');
        }
        sa.ajax('/sso/acc/updatePassword', {
          id: data.id,
          password: pass
        }, function (res) {
          layer.msg(res.msg);
        })
      });
    },
    // 变更角色
    updateRoleId: function (data, roleId, roleName) {
      if (data.id == sa.$sys.getCurUser().id) {
        return sa.alert('不能自己变更自己的角色');
      }
      if (data.roleId == roleId) {
        return sa.alert('该用户已经是' + roleName + '了');
      }
      var str = '将此账号变更为 [' + roleName + '], 请确认?';
      layer.confirm(str, {
        title: '请确认'
      }, function () {
        sa.ajax('/sso/acc/updateRole', {
          id: data.id,
          roleId: roleId
        }, function (res) {
          sa.msg(res.msg);
          data.roleId = roleId;
          data.roleName = roleName;
        });
      });
    },
    // 变更用户的状态
    updateStatus: function (data) {
      if (data.id == sa.$sys.getCurUser().id) {
        return sa.alert('不能封禁自己');
      }
      sa.ajax('/sso/acc/updateStatus', {
        id: data.id,
        status: data.status
      }, function (res) {
        sa.msg(res.msg);
      }, {
        type: 'get'
      });
    },
    // 删除
    del: function (data) {
      sa.confirm('是否删除，此操作不可撤销', function () {
        sa.ajax('/sso/acc/delete', {
          id: data.id
        }, function (res) {
          sa.arrayDelete(this.dataList, data);
          sa.ok('删除成功');
          sa.f5TableHeight(); // 刷新表格高度
        }.bind(this), {
          type: "get"
        })
      }.bind(this));
    },
    // 批量删除
    deleteByIds: function () {
      // 获取选中元素的id列表
      let selection = this.$refs['data-table'].selection;
      let ids = sa.getArrayField(selection, 'id');
      if (selection.length == 0) {
        return sa.msg('请至少选择一条数据')
      }
      // 提交删除
      sa.confirm('是否批量删除选中数据？此操作不可撤销', function () {
        sa.ajax('/sso/acc/deleteByIds', {
          ids: ids
        }, function (res) {
          sa.arrayDelete(this.dataList, selection);
          sa.ok('删除成功');
          sa.f5TableHeight(); // 刷新表格高度
        }.bind(this), {
          type: 'post'
        })
      }.bind(this));
    },
  }
}
</script>

<style scoped>
.admin_oper {
  height: 100%;
  width: 100%;
  display: flex;
  justify-content: start;

  span {
    border-radius: 5px;
  }
}
</style>
